<%@page import="java.text.DecimalFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="./css/bootstrap.min.css" rel="stylesheet">
<link href="./css/dashboard.css" rel="stylesheet">
<script src="./js/jquery.min.js"></script>
<title>目录列表</title>
<%!static final String[] photos = {"zip.png", "photo.png", "exe.png"};
	static final String[][] files = {
			{".zip", ".7z", ".rar", ".tar", ".gz"}, 
			{".png", ".jpg", ".jpge", ".bmp", ".gif"},
			{".exe"}};
%>
<%
	boolean root = false;
	File path = null;
	String name = null;
	SimpleDateFormat S = new SimpleDateFormat("YYYY/MM/dd");
	try {
		if (request.getParameterValues("path") != null && request.getParameterValues("path").length == 1) {
			path = new File(request.getParameterValues("path")[0]);
			if (path.exists()) {
				root = false;
				name = path.getAbsolutePath();
			} else {
				root = true;
				name = "根目录";
			}
		} else {
			root = true;
			name = "根目录";
		}
	} catch (Exception E) {
		root = true;
		name = "根目录";
	}
	if (root)
		session.setAttribute("path", "");
	else
		session.setAttribute("path", name);
%>
</head>
<body>
	<div class="main">
		<h2 class="sub-header" id="mainPath" style="display: inline;"><%=name%></h2>
		<form action="upload" style="display: none;" method="post"
			enctype="multipart/form-data">
			<input style="display: none;" id="Upload-input" type="file"
				name="files"> <input style="display: none;"
				id="submit-input" style="display: nano" type="submit">
		</form>
		<%
			if (!root) {
		%>
		<button style="float: right;" class="btn btn-success" type="button"
			onclick="upload()">上传文件</button>
		<button style="float: right; margin-right: 10px;"
			class="btn btn-success" type="button"
			onclick="paste('<%=name.replaceAll("\\\\", "\\\\\\\\")%>')">粘贴</button>
		<%
			}
		%>
		<div class="table-responsive" style="overflow-x: visible">
			<table class="table table-striped">
				<thead>
					<tr>
						<th>名字</th>
						<th>大小</th>
						<th>修改时间</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<%
						if (root) {
							int A = 0;
							for (File F : File.listRoots()) {
					%>
					<tr>
						<td><a href="index.jsp?path=<%=F.getAbsolutePath()%>"><%=F%></a></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<%
						}
						} else {
					%>
					<tr>
						<td><img alt="" src="./image/exit.png" style="height: 18px"><a
							href="index.jsp?path=<%=path.getParent()%>"> ...</a></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<%
						for (File F : path.listFiles()) {
								if (F.isDirectory()) {
									String SS = null;
									if(F.getName().length() > 42)SS = F.getName().substring(0, 42) + "...";
									else SS = F.getName();
					%>
					<tr>
						<td title="<%=F.getName()%>"><a href="index.jsp?path=<%=F.getAbsolutePath()%>"><img
								alt="" src="./image/Folder.png" style="height: 18px"> <%=SS%></a></td>
						<td></td>
						<td><%=S.format(new Date(F.lastModified()))%></td>
						<td><a href="#"
							onclick="Delete('<%=F.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\")%>')">删除</a>
							<a href="#"
							onclick="copy('<%=F.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\")%>')">复制</a>
						</td>
					</tr>
					<%
						}
							}
					%>


					<%
						for (File F : path.listFiles()) {
								if (F.isFile()) {
									String P = null;
									String sizeString = null;
									long size = F.length();
									if (size < 1024)
										sizeString = size + "B";
									else if (size < 1024 * 1024) {
										DecimalFormat df = new DecimalFormat("#.00");
										sizeString = df.format((double) size / 1024) + "KB";
									} else if (size < 1024 * 1024 * 1024) {
										DecimalFormat df = new DecimalFormat("#.00");
										sizeString = df.format((double) size / 1024 / 1024) + "MB";
									} else {
										DecimalFormat df = new DecimalFormat("#.00");
										sizeString = df.format((double) size / 1024 / 1024 / 1024) + "MB";
									}
									for (int i = 0; i < files.length && P == null; i++)
										for (String A : files[i]) {
											if (F.getName().toLowerCase().endsWith(A)) {
												P = photos[i];
												break;
											}
										}
									if (P == null)
										P = "default.png";
									String SS = null;
									if(F.getName().length() > 42)SS = F.getName().substring(0, 42) + "...";
									else SS = F.getName();
					%>
					<tr>
						<td title="<%=F.getName()%>"><img src="./image/<%=P%>" style="height: 18px"> <%=SS%></td>
						<td><%=sizeString%></td>
						<td><%=S.format(new Date(F.lastModified()))%></td>
						<td><a href="#"
							onclick="Delete('<%=F.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\")%>')">删除</a>
							<a href="#"
							onclick="copy('<%=F.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\")%>')">复制</a>
							<a href="#"
							onclick="goDownload('<%=F.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\")%>')">下载</a>
						</td>
					</tr>
					<%
						}
							}
						}
					%>
				</tbody>
			</table>
		</div>
		<footer>
			<p>&copy; Company 2016 by huaji</p>
		</footer>
	</div>
	<a id="download" style="display: none;" target="_black"></a>
	<script type="text/javascript">
		function upload() {
			$("#Upload-input").click().change(function() {
				$("#submit-input").click();
			});
		}
	</script>
	<script type="text/javascript">
		function paste(S) {
			if (confirm("你确定要粘贴?(可能会覆盖现有文件!)")) {
				$.post("Method", {
					method : "paste()",
					args : S
				}, function(data, status) {
					if (data != "true")
						alert("错误" + data);
					else
						location.reload();
				});
			}
		}
		function goDownload(S) {
			$("#download").attr("href","download?path=" + S);
			document.getElementById("download").click();
		}
		function Delete(S) {
			if (confirm("你确定要删除?")) {
				$.post("Method", {
					method : "delete()",
					args : S
				}, function(data, status) {
					if (data != "true")
						alert("错误" + data);
					else
						location.reload();
				});
			}
		}
		function copy(S) {
			$.post("Method", {
				method : "copy()",
				args : S
			}, function(data, status) {
				if (data != "true")
					alert("错误: " + data);
				else
					alert("已复制到剪切板");
			});
		}
	</script>
</body>
</html>